/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: jse2204 - ThreadDemo
 * <p>Powered by webrx On 2022-09-29 09:43:34
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ThreadDemo {

    public static void show(){
        while(true){
            try{
                TimeUnit.SECONDS.sleep(2);
            }catch(Exception e){
                e.printStackTrace();
            }
            System.out.printf("%s:%n",Thread.currentThread().getName());
        }
    }

    public static void main(String[] args) {
        T2 t2 = new T2();
        Thread t = new Thread(t2,"第二线程");
        t.start();


        //使用lambda表达式实现Runnable接口
        Runnable tt = ()->{
            while(true){
                try{
                    TimeUnit.SECONDS.sleep(2);
                }catch(Exception e){
                    e.printStackTrace();
                }
                System.out.printf("%s:%n",Thread.currentThread().getName());
            }
        };
        Thread t3 = new Thread(tt,"t3");
        t3.start();


        new Thread(ThreadDemo::show,"s1").start();

        System.out.println(Thread.activeCount());
    }

    public static void t1(String[] args) {
        //Thread-0
        Thread t  = new T1(); //实例化线程类
        t.setName("TTA"); //设置当前线程实现的名称,如果不设置，第一个线程为 thread-0
        t.start();

        //Thread-1
        Thread t2  = new T1();
        t2.setName("TTB");
        t2.start();
    }
}
